﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.IO;

namespace McMySkin_Installer
{
    public class McMySkinLogin
    {
        //创建实例
        public static McMySkinLogin Instance = new McMySkinLogin();

        //获取皮肤的地址
        public const string MMS_URL = "http://www.mcmyskin.com/api/process.php?action={0}&email={1}&password={2}";
        public const string MMS_DATA_URL = "http://www.mcmyskin.com/api/getdata.php?sid={0}&property={1}";

        public string DoLogin(string email, string password)
        {
            string passwordMd5 = Unit.GetMD5(password);

            string url = MMS_URL.Replace("{0}", "login").Replace("{1}",email).Replace("{2}",passwordMd5);

            try
            {
                string back = Unit.ReadURL(url, string.Empty, Encoding.UTF8);

                if (!string.IsNullOrEmpty(back))
                {
                    string[] ss = back.Split(',');


                    if (ss[0] == "2")
                    {
                        LoginStatus.Login = true;
                        LoginStatus.Session_id = ss[2];

                        UpdateInfo();

                        return ss[1];
                    }
                    else
                    {
                        return ss[1];
                    }
                }
                else
                {
                    return "未知错误！，未能从MMS取得数据。(Return is null)";
                }
            }
            catch(System.Net.WebException)
            {
                return "网络错误，请检查网络连接！";
            }

        }

        public void DoLogout(string email, string password)
        {
            if (LoginStatus.Login)
            {
                string passwordMd5 = Unit.GetMD5(password);
                string url = MMS_URL.Replace("{0}", "quit").Replace("{1}", email).Replace("{2}", passwordMd5);

            }
        }

        public bool UpdateInfo()
        {

            try
            {
                string url = MMS_DATA_URL.Replace("{0}", LoginStatus.Session_id).Replace("{1}", "muti&muti=uid,email,nickname,mcids");
                string backJson = Unit.ReadURL(url, null, Encoding.UTF8);

                string[] values = new string[4];

                using (JsonTextReader reader = new JsonTextReader(new StringReader(backJson)))
                {
                    int i = 0;

                    while (reader.Read())
                    {
                        if (reader.Value != null)
                        {
                            if (reader.TokenType == JsonToken.String)
                            {
                                string s = reader.Value.ToString();

                                values[i] = s;
                                i++;
                            }
                        }
                    }
                }

                LoginStatus.Uid = values[0];
                LoginStatus.NickName = values[2];
                LoginStatus.SkinUserName = values[3];

                return true;
            }
            catch (System.Net.WebException)
            {
                return false;
            }
        }

        public bool CheckLogin()
        {
            string url = MMS_DATA_URL.Replace("{0}",LoginStatus.Session_id).Replace("{1}","uid");
            string back = Unit.ReadURL(url, null, Encoding.UTF8);
            if (!string.IsNullOrEmpty(back))
            {
                LoginStatus.Login = true;
                LoginStatus.Uid = back;

                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
